package com.cn.web.controller.sys;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.cn.bean.dto.DataStructureDto;
import com.cn.bean.dto.StatusOperationDto;
import com.cn.bean.pojo.sys.BusinessStateOperationSub_Table;
import com.cn.bean.pojo.sys.BusinessStatusOperationMainTable;
import com.cn.bean.pojo.sys.DataStructureMainTable;
import com.cn.bean.pojo.sys.DataStructureSub_Table;
import com.cn.config.config.Result;
import com.cn.service.server.sys.DataStructureService;
import com.cn.service.server.sys.StatusOperationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Slf4j
public class StatusOperationController {
    @Autowired
    private StatusOperationService statusOperationService;

    @RequestMapping("/select/statusOperation")
    @ResponseBody
    public List<StatusOperationDto> selectStatusOperation(@RequestBody StatusOperationDto statusOperationDto) {
        List<StatusOperationDto> assists = statusOperationService.selectStatusOperation(statusOperationDto);
        return assists;
    }
    @RequestMapping("/select/statusOperationAll")
    @ResponseBody
    public List<StatusOperationDto> selectStatusOperationAll(@RequestBody StatusOperationDto statusOperationDto) {
        List<StatusOperationDto> selectedAll = statusOperationService.selectAll(statusOperationDto);
        return selectedAll;
    }
    @RequestMapping("/insert/statusOperation")
    @ResponseBody
    public Result insertStatusOperation(@RequestBody JSONObject jsonObject) {
//        DataStructureDto dataStructureDto = jsonObject.getObject("requestData", DataStructureDto.class);
        JSONArray dataArray = jsonObject.getJSONArray("requestData");
//        log.debug("dataStructureDto.getChineseName="+dataStructureDto.getChineseName());
        BusinessStatusOperationMainTable mainTable = new BusinessStatusOperationMainTable();
        Integer id = statusOperationService.getStatusOperationM();
        id = id != null ? ++id : 1;
        String tableName = jsonObject.getString("tableName");
       String operationField=jsonObject.getString("operationField");
         String operationMenu=jsonObject.getString("operationMenu");
      String operationButton=jsonObject.getString("operationButton");
        String direction=jsonObject.getString("direction");
       String precedingOperation=jsonObject.getString("precedingOperation");;
        String subsequentOperation=jsonObject.getString("subsequentOperation");;
        mainTable.setId(id);
        mainTable.setSequenceNumber(id.toString());
        mainTable.setOperationButton(operationButton);
        mainTable.setOperationField(operationField);
        mainTable.setOperationMenu(operationMenu);
        mainTable.setPrecedingOperation(precedingOperation);
        mainTable.setSubsequentOperation(subsequentOperation);
        mainTable.setDirection(direction);
        mainTable.setTableName(tableName);
        int a = statusOperationService.insertStatusOperationMainTable(mainTable);
        if (a != 1) {
            return Result.failure("500");
        }
        if (dataArray != null && !dataArray.isEmpty()) {
            List<StatusOperationDto> djBhList = dataArray.toJavaList(StatusOperationDto.class);
            for (StatusOperationDto dataStructureDto : djBhList) {
                BusinessStateOperationSub_Table subTable = new BusinessStateOperationSub_Table();
                BeanUtils.copyProperties(dataStructureDto, subTable);
                Integer autoid = statusOperationService.getStatusOperationSub();
                autoid = autoid != null ? ++autoid : 1;
                subTable.setId(autoid);
                subTable.setMid(id);
                subTable.setSequenceNumber(autoid.toString());
                int b = statusOperationService.insertStateOperationSubTable(subTable);
                if (b != 1) {
                    return Result.failure("500");
                }
            }
        }

        return Result.success(200);
    }
    @RequestMapping("/del/statusOperation")
    @ResponseBody
    public Result delStatusOperation(@RequestBody StatusOperationDto statusOperationDto) {
        int id=statusOperationDto.getId();
        List<StatusOperationDto> dataStructureDtos=statusOperationService.selectSub(id);
        int c=statusOperationService.delStatusOperationMain(id);
        if (dataStructureDtos.size()>0){
            int d=statusOperationService.delStatusOperationSub(id);
        }
        if (c!=1){
            return Result.failure("500");
        }
        return Result.success(200);
    }
    @RequestMapping("/update/statusOperation")
    @ResponseBody
    public Result updateStatusOperation(@RequestBody JSONObject jsonObject) {
        JSONArray dataArray = jsonObject.getJSONArray("requestData");
//        log.debug("dataStructureDto.getChineseName="+dataStructureDto.getChineseName());
        BusinessStatusOperationMainTable mainTable = new BusinessStatusOperationMainTable();
        int id = jsonObject.getInteger("id");
        String tableName = jsonObject.getString("tableName");
        String sequenceNumber=jsonObject.getString("sequenceNumber");
        String operationField=jsonObject.getString("operationField");
        String operationMenu=jsonObject.getString("operationMenu");
        String operationButton=jsonObject.getString("operationButton");

        String precedingOperation=jsonObject.getString("precedingOperation");;
        String subsequentOperation=jsonObject.getString("subsequentOperation");;
        mainTable.setId(id);
        mainTable.setSequenceNumber(sequenceNumber);
        mainTable.setOperationButton(operationButton);
        mainTable.setOperationField(operationField);
        mainTable.setOperationMenu(operationMenu);
        mainTable.setPrecedingOperation(precedingOperation);
        mainTable.setSubsequentOperation(subsequentOperation);
        mainTable.setTableName(tableName);
        int a = statusOperationService.updateStatusOperationM(mainTable);
        if (a != 1) {
            return Result.failure("500","更新主表失败");
        }
        List<StatusOperationDto> statusOperationDtos=statusOperationService.selectSub(id);
        if (statusOperationDtos.size()>0){
            int sub = statusOperationService.delStatusOperationSub(id);
        }
        if (dataArray==null||dataArray.isEmpty()){
            return Result.success(200);
        }
        List<BusinessStateOperationSub_Table> djBhList = dataArray.toJavaList(BusinessStateOperationSub_Table.class);
        for (BusinessStateOperationSub_Table dataStructureDto : djBhList) {
            BusinessStateOperationSub_Table subTable = new BusinessStateOperationSub_Table();
            BeanUtils.copyProperties(dataStructureDto, subTable);
            Integer autoid = statusOperationService.getStatusOperationSub();
            autoid = autoid != null ? ++autoid : 1;
            subTable.setId(autoid);
            subTable.setMid(id);
            subTable.setSequenceNumber(autoid.toString());
            int b = statusOperationService.insertStateOperationSubTable(subTable);
            if (b != 1) {
                return Result.failure("500","更新子表失败");
            }
        }
        return Result.success(200);
    }
}

